﻿package com.gengine.utils
{
    import com.gengine.debug.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class FilterText extends EventDispatcher
    {
        private const MAX_LENGTH:int = 1000;
        private var _loader:URLLoader;
        private var _filterStr:String;
        private var _filterRegExp:RegExp;
        private var _splitReg:RegExp;
        private var _wordMap:Dictionary;
        private var _filterRegAry:Array;
        private static var _instance:FilterText;

        public function FilterText()
        {
            this._splitReg = /(\
n|\r)+""(\n|\r)+/mg;
            this._filterRegAry = [];
            this._loader = new URLLoader();
            this._wordMap = new Dictionary();
            return;
        }// end function

        public function load(param1:String) : void
        {
            var file:* = param1;
            var request:* = new URLRequest(file);
            try
            {
                this._loader.load(request);
                this.addListener();
            }
            catch (error:Error)
            {
                Log.system("Unable to load requested document.");
            }
            return;
        }// end function

        private function addListener() : void
        {
            this._loader.addEventListener(Event.COMPLETE, this.completeHandler);
            this._loader.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
            return;
        }// end function

        private function removelistener() : void
        {
            this._loader.removeEventListener(Event.COMPLETE, this.completeHandler);
            this._loader.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
            return;
        }// end function

        private function completeHandler(event:Event) : void
        {
            this.log("加载完成");
            this.removelistener();
            var _loc_2:* = URLLoader(event.target);
            this.createRegExpStr(String(_loc_2.data));
            dispatchEvent(event);
            return;
        }// end function

        private function createRegExpStr(param1:String) : void
        {
            var _loc_2:* = param1.split(this._splitReg);
            var _loc_3:* = _loc_2.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                this.addWord(_loc_2[_loc_4]);
                _loc_4++;
            }
            return;
        }// end function

        public function ioErrorHandler(event:IOErrorEvent) : void
        {
            Log.system(event);
            dispatchEvent(event);
            return;
        }// end function

        public function getFilterStr(param1:String) : String
        {
            var _loc_3:String = null;
            var _loc_4:* = undefined;
            if (param1 == null || param1 == "")
            {
                return "";
            }
            var _loc_2:* = param1.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_2)
            {
                
                _loc_3 = param1.charAt(_loc_5);
                if (_loc_3 != "*")
                {
                    _loc_4 = this._wordMap[_loc_3];
                    if (_loc_4 is String)
                    {
                        var _loc_6:* = new RegExp("(" + _loc_4 + ")", "img");
                        this._wordMap[_loc_3] = new RegExp("(" + _loc_4 + ")", "img");
                        _loc_4 = _loc_6;
                    }
                    param1 = param1.replace(_loc_4, this.regHandler);
                }
                _loc_5++;
            }
            return param1;
        }// end function

        public function setFilterColorStr(param1:String, param2:String = "#FF0000") : String
        {
            var _loc_4:String = null;
            var _loc_5:* = undefined;
            if (param1 == null || param1 == "")
            {
                return "";
            }
            var _loc_3:* = param1.length;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_3)
            {
                
                _loc_4 = param1.charAt(_loc_6);
                if (_loc_4 != "*")
                {
                    _loc_5 = this._wordMap[_loc_4];
                    if (_loc_5 is String)
                    {
                        var _loc_7:* = new RegExp("(" + _loc_5 + ")", "img");
                        this._wordMap[_loc_4] = new RegExp("(" + _loc_5 + ")", "img");
                        _loc_5 = _loc_7;
                    }
                    param1 = param1.replace(_loc_5, "<font color=\'" + param2 + "\'>$1</font>");
                }
                _loc_6++;
            }
            return param1;
        }// end function

        public function isHaveFilterTxt(param1:String) : Boolean
        {
            var _loc_2:* = this.getFilterStr(param1);
            return _loc_2 != param1;
        }// end function

        private function regHandler() : String
        {
            arguments = arguments[1].toString();
            return arguments.replace(/.{1}"".{1}/g, "*");
        }// end function

        private function log(param1:Object) : void
        {
            Log.system(param1);
            return;
        }// end function

        public function setFilterStr(param1:String, param2:RegExp = null) : void
        {
            if (param2 != null)
            {
                this._splitReg = param2;
            }
            this.createRegExpStr(param1);
            return;
        }// end function

        public function clearWords() : void
        {
            this._wordMap = new Dictionary();
            return;
        }// end function

        public function addWord(param1:String) : void
        {
            var _loc_2:String = null;
            var _loc_3:String = null;
            param1 = param1.replace("\n", "");
            if (param1 != null && param1.length > 0)
            {
                _loc_3 = param1.charAt(0);
                _loc_2 = this._wordMap[_loc_3] as String;
                if (_loc_2)
                {
                    this._wordMap[_loc_3] = this._wordMap[_loc_3] + ("|" + param1);
                }
                else
                {
                    this._wordMap[_loc_3] = param1;
                }
            }
            return;
        }// end function

        public static function get instance() : FilterText
        {
            if (_instance == null)
            {
                _instance = new FilterText;
            }
            return _instance;
        }// end function

    }
}
